package com.zxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.dao.UserMapper;
import com.zxy.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserMapper userMapper;
    /**
     * 跳转前端页面
     * @return
     */
    @RequestMapping("userList")
    public String toString() {
        return "user/userList";
    }
    /**
     * 跳转新增页面
     * @return
     */
    @RequestMapping("toAddPage")
    public String toAddPage() {
        return "user/userAdd";
    }
    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping("toEditPage")
    public String toEditPage(Model model,String id) {
        model.addAttribute("user", userMapper.selectById(id));
        return "user/userEdit";
    }

    /**
     * 获取UserList数据
     * @return
     */
    @RequestMapping("getData")
    @ResponseBody
    public Map<String, Object> getData(HttpServletRequest request) {
        String id = request.getParameter("key[id]");

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(id)){
            queryWrapper.like(true, "id", id);
        }
        List<User> userList = userMapper.selectList(queryWrapper);

        // 组装layui table需要的数据格式
        /*  {
            "code": 0,
                "msg": "",
                "count": 1000,
                "data": [{}, {}]
        }*/
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", userList.size());
        map.put("data", userList);
        return map;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        System.out.println(ids);

        QueryWrapper<User> w = new QueryWrapper<>();
        w.in("id", ids.split(","));


        // 组装layui table需要的数据格式
        /*  {
            "code": 0,
                "msg": "",
                "count": 1000,
                "data": [{}, {}]
        }*/
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        try{
            int count = userMapper.delete(w);
            if(count == 0){
                throw new Exception("删除失败");
            }
        }catch (Exception e){
            map.put("code", -1);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(User user) {


        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        try{
            user.setPassword("123123");
            int count = userMapper.insert(user);
            if(count == 0){
                throw new Exception("新增失败");
            }
        }catch (Exception e){
            map.put("code", -1);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * edit
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Map<String, Object> edit(User user) {


        Map<String, Object> map = new HashMap<>();
        try{
            map.put("code", 0);
            map.put("msg", "success");
            int count = userMapper.updateById(user);
            if(count == 0){
                throw new Exception("更新失败");
            }
        }catch (Exception e){
            map.put("code", -1);
            map.put("msg", e.getMessage());
        }
        return map;
    }
}
